// ks/src/main/java/com/example/ks/dao/impl/OrderDaoImpl.java
package com.example.ks.dao.impl;

import com.example.ks.dao.OrderDao;
import com.example.ks.entity.Order;
import com.example.ks.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl implements OrderDao {

    @Override
    public int createOrder(Order order) throws SQLException {
        String sql = "INSERT INTO order_table (admin_id, user_id, service_start_time, service_item, pet_name, status) VALUES (?, ?, ?, ?, ?, ?)";
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        int orderId = -1;

        try {
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            pstmt.setInt(1, order.getAdminId());
            pstmt.setInt(2, order.getUserId());
            pstmt.setTimestamp(3, new Timestamp(order.getStartTime().getTime()));
            pstmt.setString(4, order.getServiceItem());
            pstmt.setString(5, order.getPetName());
            pstmt.setInt(6, 0); // 设置初始状态为0（未开始）

            System.out.println("SQL: " + sql);
            System.out.println("Params: " + order.getAdminId() + ", " + order.getUserId() + ", " + order.getStartTime() + ", " + order.getServiceItem() + ", " + order.getPetName() + ", 0");

            pstmt.executeUpdate();

            rs = pstmt.getGeneratedKeys();
            if (rs.next()) {
                orderId = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("Database error: " + e.getMessage());
            throw e;
        } finally {
            DBUtil.close(rs, pstmt, conn);
        }
        return orderId;
    }

    @Override
    public List<Order> getAllOrders() throws SQLException {
        String sql = "SELECT * FROM order_table";
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Order> orders = new ArrayList<>();

        try {
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                Order order = new Order();
                order.setId(rs.getInt("id"));
                order.setAdminId(rs.getInt("admin_id"));
                order.setUserId(rs.getInt("user_id"));
                order.setStartTime(rs.getTimestamp("service_start_time"));
                order.setServiceItem(rs.getString("service_item"));
                order.setPetName(rs.getString("pet_name"));
                order.setStatus(rs.getInt("status"));
                orders.add(order);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(rs, pstmt, conn);
        }
        return orders;
    }

    @Override
    public List<Order> getOrdersByAdminId(int adminId) throws SQLException {
        String sql = "SELECT * FROM order_table WHERE admin_id = ?";
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Order> orders = new ArrayList<>();

        try {
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, adminId);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                Order order = new Order();
                order.setId(rs.getInt("id"));
                order.setAdminId(rs.getInt("admin_id"));
                order.setUserId(rs.getInt("user_id"));
                order.setStartTime(rs.getTimestamp("service_start_time"));
                order.setServiceItem(rs.getString("service_item"));
                order.setPetName(rs.getString("pet_name"));
                order.setStatus(rs.getInt("status"));
                orders.add(order);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(rs, pstmt, conn);
        }
        return orders;
    }

    @Override
    public int updateOrderStatus(int orderId, int status) throws SQLException {
        String sql = "UPDATE order_table SET status = ? WHERE id = ?";
        Connection conn = null;
        PreparedStatement pstmt = null;
        int rowsAffected = 0;

        try {
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, status);
            pstmt.setInt(2, orderId);
            rowsAffected = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(null, pstmt, conn);
        }
        return rowsAffected;
    }
    @Override
    public List<Order> getOrdersByUserId(int userId) throws SQLException {
        String sql = "SELECT * FROM order_table WHERE user_id = ?";
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Order> orders = new ArrayList<>();

        try {
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, userId);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                Order order = new Order();
                order.setId(rs.getInt("id"));
                order.setAdminId(rs.getInt("admin_id"));
                order.setUserId(rs.getInt("user_id"));
                order.setStartTime(rs.getTimestamp("service_start_time"));
                order.setServiceItem(rs.getString("service_item"));
                order.setPetName(rs.getString("pet_name"));
                order.setStatus(rs.getInt("status"));
                orders.add(order);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(rs, pstmt, conn);
        }
        return orders;
    }


}